package com.ggh.jinjilive.ui.mine.members;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentMembersBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.CommonMobile;
import com.ggh.jinjilive.mobile.Pay.WeChatPayMobile;
import com.ggh.jinjilive.mobile.mine.MembersMobile;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.members.adapter.MembersAdapter;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.utils.pay.PayUtils;
import com.ggh.jinjilive.views.GridItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MembersFragment extends BaseFragment<FragmentMembersBinding> implements AddItemOnClickListener {
    //适配器
    private MembersAdapter membersAdapter;
    //会员
    private List<MembersMobile> membersList;
    //选择中的会员
    private MembersMobile membersMobile;



    private String category;
    private List<MembersMobile> mMembersMobileList;



    public MembersFragment(String category,List<MembersMobile> mMembersMobileList){
        this.category = category;
        this.mMembersMobileList = mMembersMobileList;
    }


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_members;
    }

    @Override
    public void initView() {
        super.initView();
        membersAdapter = new MembersAdapter();
        membersAdapter.setAddItemOnClickListener(this);
        binding.rv.setLayoutManager(new GridLayoutManager(mContext,3));
        binding.rv.addItemDecoration(new GridItemDecoration(3, SizeUtils.dp2px(10),true));
        binding.rv.setAdapter(membersAdapter);
    }

    @Override
    public void initData() {
        super.initData();

        if(!TextUtils.isEmpty(category)){
            membersList = new ArrayList<>();
            for (MembersMobile membersMobile : mMembersMobileList){
                if(category.equals(membersMobile.getType())){
                    membersList.add(membersMobile);
                }
            }
            membersAdapter.setAdd(membersList);
        }
    }


    @Override
    public void onSkipClicked(int position, Object mObject) {
        membersMobile = (MembersMobile) mObject;
        for (int i =0;i < membersList.size(); i ++){
            membersList.get(i).setPitchOn(false);
        }
        membersList.get(position).setPitchOn(true);
        membersAdapter.setAdd(membersList);
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //立即付款
        binding.ButtonPayment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(null != membersMobile){
                    if(null != payChooseDialog && !payChooseDialog.isShowing()) {
                        payChooseDialog.show();
                        payChooseDialog.setPayMoneyContet(membersMobile.getPrice() +"");
                        payChooseDialog.setAddItemOnClickListener(new AddItemOnClickListener() {
                            @Override
                            public void onSkipClicked(int position, Object mObject) {
                                payChooseDialog.dismiss();
                                WeChatAlipay = position;
                                Map<String, Object> params = new HashMap<>();
                                params.put("vip_id", membersMobile.getVip_id());
                                GeneralUtils.myBuyVip(params, new AddOnClickListener1() {
                                    @Override
                                    public void onSkipClicked(Object mObject) {
                                        CommonMobile commonMobile = (CommonMobile) mObject;
                                        if(null != commonMobile){
                                            Map<String, Object> params = new HashMap<>();
                                            params.put("order_sn", commonMobile.getOrder_sn());
                                            if(WeChatAlipay == 0){//微信
                                                params.put("pay_type","wechat");
                                                GeneralUtils.WeChatMyPay(params, new AddOnClickListener1() {
                                                    @Override
                                                    public void onSkipClicked(Object mObject) {
                                                        WeChatPayMobile weChatPayMobile = (WeChatPayMobile) mObject;
                                                        if(null != weChatPayMobile){
                                                            //调起微信
                                                            PayUtils.getInstance(mContext).WeChatPay(weChatPayMobile);
                                                        }
                                                    }
                                                });
                                            }else if(WeChatAlipay == 1){//支付宝
                                                params.put("pay_type","alipay");
                                                GeneralUtils.alipayMyPay(params, new AddOnClickListener1() {
                                                    @Override
                                                    public void onSkipClicked(Object mObject) {
                                                        String AlipayPay = (String) mObject;
                                                        //调起支付宝
                                                        PayUtils.getInstance(mContext).AlipayPay(AlipayPay);
                                                    }
                                                });
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }
                }else {
                    ToastUtils.make().show("请选择会员");
                }
            }
        });
    }
}
